package com.sharemarking.system.controller;


import com.sharemarking.system.dto.Page;
import com.sharemarking.system.dto.agreement.AgreementListDto;
import com.sharemarking.system.dto.orgVerify.OrgVerifyListDto;
import com.sharemarking.system.mapper.AgreementMapper;
import com.sharemarking.system.mapper.BannerMapper;
import com.sharemarking.wa.common.entity.Agreement;
import com.sharemarking.wa.common.entity.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/agreementManagement")
public class AgreementManagementController {

    @Autowired
    private AgreementMapper agreementMapper;
    @Autowired
    private HttpSession session;

    /**
     * 列表页面
     *
     * @param search
     * @param model
     */
    @GetMapping("index")
    @PreAuthorize("hasPermission('base','all')")
    public String index(AgreementListDto obj, Integer page, Model model, Integer isSession) {
        int size = 10;
        if (page == null) {
            page = 0;
        }
        // session 参数回显
        if (isSession != null) {
            // 如果isSession为1，代表要从session取条件参数，一般应用在某操作之后要保存查询参数
            obj = session.getAttribute("query") == null ? obj : (AgreementListDto) session.getAttribute("query");
            size = session.getAttribute("size") == null ? size : (int) session.getAttribute("size");
            page = session.getAttribute("page") == null ? page : (int) session.getAttribute("page");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", page * size);
        map.put("pageSize", size);
        if (!StringUtils.isEmpty(obj.getName())){
            map.put("name", obj.getName());
        }
        if (!StringUtils.isEmpty(obj.getExamState())){
            map.put("examState", obj.getExamState());
        }
        if (!StringUtils.isEmpty(obj.getOrgName())){
            map.put("orgName", obj.getOrgName());
        }
        List<AgreementListDto> list = agreementMapper.getList(map);
        int total = agreementMapper.getListTotal(map);
        model.addAttribute("page", new Page<>(page, size, total, list));
        model.addAttribute("query", obj);
        // session 参数保存
        session.setAttribute("size", size);
        session.setAttribute("page", page);
        return "agreement/index";
    }
    /**
     * 列表页面
     *
     * @param search
     * @param model
     */
    @GetMapping("AgreementIndex")
    @PreAuthorize("hasPermission('base','all')")
    public String AgreementIndex(AgreementListDto obj, Integer page, Model model, Integer isSession) {
        int size = 10;
        if (page == null) {
            page = 0;
        }
        // session 参数回显
        if (isSession != null) {
            // 如果isSession为1，代表要从session取条件参数，一般应用在某操作之后要保存查询参数
            obj = session.getAttribute("query") == null ? obj : (AgreementListDto) session.getAttribute("query");
            size = session.getAttribute("size") == null ? size : (int) session.getAttribute("size");
            page = session.getAttribute("page") == null ? page : (int) session.getAttribute("page");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", page * size);
        map.put("pageSize", size);
        if (!StringUtils.isEmpty(obj.getName())){
            map.put("name", obj.getName());
        }
        if (!StringUtils.isEmpty(obj.getExamState())){
            map.put("examState", obj.getExamState());
        }
        if (!StringUtils.isEmpty(obj.getOrgName())){
            map.put("orgName", obj.getOrgName());
        }
        List<AgreementListDto> list = agreementMapper.getList(map);
        int total = agreementMapper.getListTotal(map);
        model.addAttribute("page", new Page<>(page, size, total, list));
        model.addAttribute("query", obj);
        // session 参数保存
        session.setAttribute("size", size);
        session.setAttribute("page", page);
        return "agreement/index_qy";
    }

    /**
     * 审核
     */
    @GetMapping("exam")
    @PreAuthorize("hasPermission('base','all')")
    @Transactional
    public String agreementExam(int id, int type, String rejectReason, Model model, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException {
        Agreement agreement = new Agreement();
        agreement.setExamState(type);
        if (type == 2) {
            if (StringUtils.isEmpty(rejectReason)) {
                redirectAttributes.addFlashAttribute("msg", "驳回理由不可为空！");
                return "redirect:index?isSession=1";
            }
            agreement.setRejectReason(rejectReason);
        } else {
            agreement.setRejectReason(null);
        }
        agreement.setId(id);
        agreement.setUpdateDate(new Date());
        agreementMapper.updateById(agreement);
        redirectAttributes.addFlashAttribute("msg", "操作成功！");
        return "redirect:index";
    }
//	/**
//	 * 新增、编辑页面
//	 * @param obj
//	 * @param model
//	 * @return
//	 */
//	@GetMapping("{id}")
//	@PreAuthorize("hasPermission('base','all')")
//	public String get(@PathVariable("id")int id, Model model){
//		Post obj = new Post();
//		if (id > 0) {
//			obj = bm.getById(id);
//		}else {
//			model.addAttribute("obj",obj);
//		}
//		model.addAttribute("obj",obj);
//		return "banner/form";
//	}
//	/**
//	 * 新增、编辑
//	 * @param obj
//	 * @param model
//	 * @return
//	 */
//	@PostMapping("{id}")
//	@PreAuthorize("hasPermission('base','all')")
//	@Transactional
//	public String post(Post obj, Model model, RedirectAttributes redirectAttributes) throws Exception{
//		if (obj.getId() == 0) {
//			bm.insert(obj);
//		}else {
//			bm.update(obj);
//		}
//		model.addAttribute("obj",obj);
//		redirectAttributes.addFlashAttribute("msg","操作成功!");
//		return "redirect:index?isSession=1";
//	}
//	/**
//	 * 删除
//	 * @param obj
//	 * @param model
//	 * @return
//	 */
//	@GetMapping("delete")
//	@PreAuthorize("hasPermission('base','all')")
//	@Transactional
//	public String delete(int id, Model model, RedirectAttributes redirectAttributes){
//		bm.delete(id);
//		redirectAttributes.addFlashAttribute("msg","操作成功！");
//		return "redirect:index?isSession=1";
//	}
}
